// pages/enterprise/home/home.js
var app = getApp();
import {
    ajaxPost,
    showToast
} from '../../../utils/util.js';
Page({
    /**
     * 页面的初始数据
     */
    data: {
        page: 0,
        limit: 10,
        selectVal: app.globalData.meetingSelectVal,
        noGet: false,
        addressVal: ['全部', '全部', '全部'],
        region: [{
                fullName: "全部",
                code: 0
            },
            {
                fullName: "全部",
                code: 0
            }
        ],
        addressShow: false,
        bannerSource: [],
        footerSource: [{
                title: '选选',
                url: '/pages/student/list/list',
                img: '/images/icon_footer_icon1.png',
                imgSet: '/images/icon_footer_icon1_set.png',
                select: false
            },
            {
                title: '招聘会',
                url: '',
                img: '/images/icon_footer_icon2.png',
                imgSet: '/images/icon_footer_icon2_set.png',
                select: true
            },
            {
                title: '我的',
                url: '/pages/student/mine/mine',
                img: '/images/icon_footer_icon3.png',
                imgSet: '/images/icon_footer_icon3_set.png',
                select: false
            },
        ],
        listSource: []
    },
    /**
     * 调用省市区三级联动
     */
    addressClick(e) {
        console.log();
        this.setData({
            [e.currentTarget.dataset.type]: true
        })
    },
    /**
     * 省市区三级联动 确认回调
     */
    addressSelect(e) {
        let selectVal = e.detail.selectVal
        console.log('addressSelect', selectVal);
        this.setData({
            region: selectVal
        })
        this.setData({
            page: 0,
            noGet: false,
            listSource: [],
            selectVal: e.currentTarget.dataset.type,
        })
        this.getList()

    },
    clickSelect(e) {
        if (e.currentTarget.dataset.type !== this.data.selectVal) {
            this.setData({
                page: 0,
                noGet: false,
                listSource: [],
                selectVal: e.currentTarget.dataset.type,
            })
            this.getList()
        }
    },

    getList() {
        let _this = this;
        if (this.data.noGet) {
            return;
        }
        let data = {
            token: wx.getStorageSync('token'),
            page: ++this.data.page,
            limit: this.data.limit,
            source: 'wx',
            type: this.data.selectVal,
            is_follow: 0,
            province: this.data.region[0].fullName,
            city: this.data.region[1].fullName,

        };
        if (!this.data.selectVal) {
            data.type = 1
        }
        ajaxPost("/meeting/meetinglist", data, function (res) {
            if (res.code == 0) {
                let data = res.data;
                if (data.length == 0) {
                    _this.setData({
                        noGet: true
                    })
                } else {
                    let newarr = _this.data.listSource.concat(data)
                    _this.setData({
                        listSource: newarr,
                    })
                }
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: "none",
                    duration: 2000
                });
            }
        });
    },
    goCont(e) {
        let index = e.currentTarget.dataset.index
        let data = this.data.listSource[index]

        if ((data.status == 1 || data.status == 2) && this.data.selectVal == 1) {
            wx.navigateTo({
                url: "/pages/student/hallRoom/hallRoom?id=" + data.id + '&roomid=' + data.roomid
            });
        }
        if (data.status !== 3 && this.data.selectVal == 2) {
            let studentInfo = this.data.listSource[index]
            wx.navigateTo({
                url: "/pages/student/code/code?id=" + studentInfo.id
            });
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            page: 0,
            listSource: [],
            noGet: false,
        })
        this.getList()
        this.getBanner()
    },

    /**
     * 
     */
    getBanner() {
        let data = {
            token: wx.getStorageSync('token')
        }
        ajaxPost('/index/banner', data, (res) => {
            if (res.code == 0) {
                this.setData({
                    bannerSource: res.data
                })
            }
        })
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getList()
    },
    /**
     * 关注按钮
     */
    clickFollowed(e) {
        let index = e.currentTarget.dataset.index
        let data = this.data.listSource[index]
        let ajaxData = {
            token: wx.getStorageSync('token'),
            meeting_id: data.id,
        }
        if (data.is_follow == 0) {
            ajaxPost('/meeting/followmeeting', ajaxData, (res) => {
                if (res.code == 0) {
                    const {
                        listSource
                    } = this.data
                    let newListSource = listSource
                    newListSource[index].follow_count = newListSource[index].follow_count + 1
                    newListSource[index].is_follow = 1
                    this.setData({
                        listSource: newListSource
                    })
                    showToast('success', res.msg);
                } else {
                    showToast('text', res.msg);
                }
            })

        } else {
            ajaxPost('/meeting/cancelfollowmeeting', ajaxData, (res) => {
                if (res.code == 0) {
                    const {
                        listSource
                    } = this.data
                    let newListSource = listSource
                    newListSource[index].follow_count = newListSource[index].follow_count - 1
                    newListSource[index].is_follow = 0
                    this.setData({
                        listSource: newListSource
                    })
                    showToast('success', res.msg);
                } else {
                    showToast('text', res.msg);
                }
            })

        }

    },
    /**
     * 地址更改
     */
    addressChange(e) {
        let val = e.detail.value,
            addressVal = this.data.addressVal
        console.log('picker发送选择改变，携带值为', e.detail.value)
        if (val[0] == addressVal[0] && val[2] == addressVal[2] && val[3] == addressVal[3]) {
            return;
        }
        this.setData({
            addressVal: val
        })
        this.setData({
            page: 0,
            noGet: false,
            listSource: []
        })
        this.getList()
    }
})